<?php

namespace App\Repositories\Eloquent\Acl;

use App\Repositories\Eloquent\Repository;

class PermissionRepository extends Repository
{

    /**
     * 指定模型名称
     *
     * @return mixed
     */
    function model()
    {
        return 'App\Models\Acl\Permission';
    }

    /**
     * permission列表
     *
     * @param string $query
     * @return mixed
     */
    public function index()
    {
        return $this->model->orderBy('order');
    }

    /**
     * 查找指定permission
     *
     * @param $id
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function show($id)
    {
        $permission = $this->find($id); 
        if (!$permission) 
            return $this->responseFail(404);
        return $permission;        
        
    }

    /**
     * 存储permission
     *
     * @param $request
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function store($request)
    {        
        $result = $this->create([
            'name' => $request->name,
            'title' => $request->title,
            'parent' => $request->isparent == 1 ? 0:$request->parent,
            'controller' => $request->controller,
            'action' => $request->action,            
            'icon' => $request->icon,
            'order' => $request->order,
            'status' => STATUS_NORMAL
        ]);
        if (!!$result) {
            return $this->responseSuccess('添加权限成功', $$result, route('system.permission.index'));
        }            

        return $this->responseFail(500, '添加权限失败');
    }

    /**
     * 更新permission
     *
     * @param $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function renew($request, $id)
    {
        $result = $this->update([
            'name' => $request->name,
            'title' => $request->title,
            'parent' => $request->parent,
            'controller' => $request->controller,
            'action' => $request->action,
            'status' => $request->status,
            'using' => $request->using,
            'icon' => $request->icon,
            'order' => $request->order,
        ], $id);
                 
          
        if(!!$result)
            return $this->responseSuccess('更新权限成功', $result, route('system.permission.index'));
       
                     
        return $this->responseFail(500, '更新权限失败');
    }

    /**
     * 删除permission
     *
     * @param $ids
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function destroy($ids)
    {
        return $this->respondWith(['deleted' => !!$this->delete($ids)]);
    }


    public function parents()
    {
        return $this->model->where('parent', 0)->get();
    }


    /**
     * [get description]
     * @return [type] [description]
     */
    public function get($status = null)
    {
        if(isset($status)){
            $rows = $this->model->where('status', $status)->orderBy('order', 'ASC')->get();
        }
        else{
            $rows = $this->model->orderBy('order', 'ASC')->get();
        }
        

        return $this->children($rows);
    }

    /**
     * [children description]
     * @param  [type]  $data [description]
     * @param  integer $pid  [description]
     * @return [type]        [description]
     */
    public function children($data, $pid = 0)    
    {
        $tree = array();
        foreach($data as $key => $value) {            
            if($value['parent'] === $pid) { 
                $value['children'] = $this->children($data, $value['id']);
                $tree[] = $value;
            }
        }
        return $tree;
    }
}